<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">
<META NAME="Generator" CONTENT="Microsoft Word 97">
<TITLE>USING THE DATABASE MANAGER</TITLE>
</HEAD>
<BODY LINK="#0000ff" VLINK="#800080">

<FONT FACE="Verdana"><PRE>

USING THE DATABASE MANAGER

Once a connection is established to a database in any mode, 
JDBC methods are used to interact with the database. Use the 
jdbcConnection, jdbcDriver, jdbcDatabaseMetadata, jdbcResultSet, 
jdbcStatement and jdbcPreparedStatement. 
The database access methods use SQL commands to perform actions 
on the database and return the results either as a Java primitive 
type or as an instance of the 
br.com.dreamsource.mobile.jmesql.jdbcResultSet class.

The SQL dialect used in jMeSQL is as close to the SQL92 standard 
as it has been possible to achieve so far in a small footprint 
database engine. The full list of SQL commands is in jMeSQL 
Syntax documentation.

THE CONNECTION SAMPLE

import br.com.dreamsource.mobile.jmesql.exceptions.SQLException;
import br.com.dreamsource.mobile.jmesql.io.File;
import br.com.dreamsource.mobile.jmesql.io.Properties;
import br.com.dreamsource.mobile.jmesql.jdbcConnection;
import br.com.dreamsource.mobile.jmesql.jdbcDriver;
import br.com.dreamsource.mobile.jmesql.jdbcPreparedStatement;
import br.com.dreamsource.mobile.jmesql.jdbcResultSet;
import br.com.dreamsource.mobile.jmesql.jdbcStatement;
import java.util.Enumeration;
import java.util.Vector;

public class DatabaseManager {

       /* 
        * the database connection
        */
    private static jdbcConnection connection;

    /*
     * Properties contains ("user", "password", "database")
     */
    private static Properties props;
    
    public DatabaseManager(String databaseName, String userName, String passwd) throws SQLException {
        //Setting the properties
        props = new Properties();
        props.setProperty("user", userName);
        props.setProperty("password", passwd);
        props.setProperty("database", databaseName);
&#9;
        if (this.connection == null) {
            jdbcDriver driver = new jdbcDriver();

&#9;    /*
&#9;     * If the database does not exists, 
&#9;     * the dbms create a new, default 
&#9;     * user name is "SA" and 
&#9;     * password is empty 
&#9;    */

            this.connection = driver.connect(this.props);
        }
    } 
    
    private jdbcConnection getConnection() throws SQLException {
        if (this.connection == null) {
            throw new SQLException("The connection is null!");
        }
        return this.connection;
    }
    
    private jdbcPreparedStatement getPreparedStatement(String sqlValue) throws SQLException {
        return this.getConnection().prepareStatement(sqlValue);
    }
    
    /*
    * Execute de SELECT statement
</FONT><FONT FACE="Verdana" COLOR="#ff0000">    * Ex: String sqlValue = "SELECT * FROM MYTABLE"
    *     jdbcResultSet resultSet = myDatabaseManager.list(sqlValue);</FONT><FONT FACE="Verdana">   
    */
    public jdbcResultSet list(String sql) throws SQLException {
        jdbcPreparedStatement prepared = this.getPreparedStatement(sql);
        return prepared.executeQuery();
    }

    /*
    * Execute de SELECT statement based in Vector attributes
</FONT><FONT FACE="Verdana" COLOR="#ff0000">    * Ex: String sqlValue = "SELECT * FROM MYTABLE WHERE ID = ?"
    *     Vector values = new Vector();
    *     values.addElement("1");
    *     jdbcResultSet resultSet = myDatabaseManager.find(sqlValue, values);</FONT><FONT FACE="Verdana">   
    */
    public jdbcResultSet find(String sqlValue, Vector values) throws SQLException {
        jdbcPreparedStatement preparedStatement = this.getPreparedStatement(sqlValue);
        for (int i = 0; i &lt; values.size(); i++) {
            preparedStatement.setString(i + 1, (String)values.elementAt(i));
        }        
&#9;return preparedStatement.executeQuery();
    }

    /*
    * Execute de INSERT statement based in Vector attributes
</FONT><FONT FACE="Verdana" COLOR="#ff0000">    * Ex: String sqlValue = "INSERT INTO MYTABLE (ID, NAME) VALUES (?, ?)"
    *     Vector values = new Vector();
    *     values.addElement("1");
    *     values.addElement("aaa");
    *     myDatabaseManager.insert(sqlValue, values);</FONT><FONT FACE="Verdana">   
    */
    public boolean insert(String sqlValue, Vector values) throws SQLException {
        return this.executeUpdateSQL(sqlValue, values);
    }

    /*
    * Execute de UPDATE statement based in Vector attributes
</FONT><FONT FACE="Verdana" COLOR="#ff0000">    * Ex: String sqlValue = "UPDATE MYTABLE SET ID = ?, NAME = ? WHERE ID = ?"
    *     Vector values = new Vector();
    *     values.addElement("2");
    *     values.addElement("aaa");
    *     values.addElement("1");
    *     myDatabaseManager.update(sqlValue, values);</FONT><FONT FACE="Verdana">   
    */
    public boolean update(String sqlValue, Vector values) throws SQLException {
        return this.executeUpdateSQL(sqlValue, values);
    }

    /*
    * Execute de UPDATE statement based in Vector attributes
</FONT><FONT FACE="Verdana" COLOR="#ff0000">    * Ex: String sqlValue = "DELETE FROM WHERE ID = ?"
    *     Vector values = new Vector();
    *     values.addElement("2");
    *     myDatabaseManager.delete(sqlValue, values);</FONT><FONT FACE="Verdana">   
    */
    public boolean delete(String sqlValue, Vector values) throws SQLException {
        return this.executeUpdateSQL(sqlValue, values);
    }

    private boolean executeUpdateSQL(String sqlValue, Vector values) throws SQLException {
        jdbcPreparedStatement preparedStatement = this.getPreparedStatement(sqlValue);
        for (int i = 0; i &lt; values.size(); i++) {
            preparedStatement.setString(i + 1, (String)values.elementAt(i));
        }
        if (preparedStatement.executeUpdate() &gt; 0) {
            return true;
        } else {
            return false;
        }
    }
    
}

The red text is sample use in your midlet.</PRE></FONT></BODY>
</HTML>